package com.ruoyi.tester.executor.expression;

import com.ruoyi.tester.executor.ExpressionContext;

import java.util.Calendar;

/**
 * @author SongJun
 * @description 时间相关处理抽象类, 完成时间的计算, 子类转换为对应的格式
 * @date 2021/6/10 16:42
 */
public abstract class AbstractExpTime implements ExpressionAnalysis{
    @Override
    public Object analysis(ExpressionContext context, String... param) {
        Calendar calendar = Calendar.getInstance();
        if(param != null && param.length > 1){
            String type = param[0];
            String intervalStr = param[1];
            Integer interval = Integer.valueOf(intervalStr);
            if("year".equalsIgnoreCase(type) || "year".equalsIgnoreCase(type)){
                calendar.add(Calendar.YEAR, interval);
            } else if("month".equalsIgnoreCase(type) || "mo".equalsIgnoreCase(type)){
                calendar.add(Calendar.MONTH, interval);
            } else if("day".equalsIgnoreCase(type) || "d".equalsIgnoreCase(type)){
                calendar.add(Calendar.DATE, interval);
            } else if("hour".equalsIgnoreCase(type) || "h".equalsIgnoreCase(type)){
                calendar.add(Calendar.HOUR, interval);
            } else if("minute".equalsIgnoreCase(type) || "mi".equalsIgnoreCase(type)){
                calendar.add(Calendar.MINUTE, interval);
            } else if("second".equalsIgnoreCase(type) || "s".equalsIgnoreCase(type)){
                calendar.add(Calendar.SECOND, interval);
            }
        }
        return calendar;
    }

    @Override
    public String getDescription(){
        return "可接受两个参数, 表示增加相应的时间, 如"+getExpressionName()+"(“day”,1)表示当前时间增加1天;可接受“year”, “month”, “day”, “hour”, “minute”, “second”这几种类型;";
    }
}
